using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using SEO.Model;
using SEO.DBSqlHelp;

namespace SEO.DAL
{

    /// <summary>
    /// 版权所有: 版权所有(C) 2011，上海云里网络科技有限公司
    /// 内容摘要: EDMMailHistoryDAL
    /// 完成日期：2011年7月29日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class EDMMailHistoryDAL
    {

        /// <summary>
        /// 获取EDMMailHistory表中数据
        /// <returns>返回List</returns>
        /// </summary>
        public List<EDMMailHistory> GetAllEDMMailHistoryDAL()
        {
            List<EDMMailHistory> list = new List<EDMMailHistory>();
            string strSql = "select * from EDMMailHistory";
            try
            {
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    EDMMailHistory ey = new EDMMailHistory();
                    ey.HistoryId = int.Parse(dr["history_id"].ToString());
                    ey.SuccessNum = int.Parse(dr["success_num"].ToString());
                    ey.FailNum = int.Parse(dr["fail_num"].ToString());
                    ey.InvalidNum = int.Parse(dr["invalid_num"].ToString());
                    ey.AddDate = dr["add_date"].ToString();
                    ey.UserId = int.Parse(dr["user_id"].ToString());

                    list.Add(ey);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMailHistory -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 根据指定SQL语句获取EDMMailHistory表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回List</returns>
        public List<EDMMailHistory> GetEDMMailHistoryByWhereDAL(string strSql)
        {
            List<EDMMailHistory> list = new List<EDMMailHistory>();
            try
            {
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataReader dr = cm.ExecuteReader();
                while (dr.Read())
                {
                    EDMMailHistory ey = new EDMMailHistory();
                    ey.HistoryId = int.Parse(dr["history_id"].ToString());
                    ey.SuccessNum = int.Parse(dr["success_num"].ToString());
                    ey.FailNum = int.Parse(dr["fail_num"].ToString());
                    ey.InvalidNum = int.Parse(dr["invalid_num"].ToString());
                    ey.AddDate = dr["add_date"].ToString();
                    ey.UserId = int.Parse(dr["user_id"].ToString());

                    list.Add(ey);
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMailHistory -> " + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 往EDMMailHistory表中插入数据
        /// <param name="ey">实例化EDMMailHistory类</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int InsertEDMMailHistoryDAL(SEO.Model.EDMMailHistory ey)
        {
            int iNum = 0;
            string strSql = "insert into EDMMailHistory(history_id,success_num,fail_num,invalid_num,add_date,user_id) " +
             " values(" + ey.HistoryId + "," + ey.SuccessNum + "," + ey.FailNum + "," + ey.InvalidNum + ",'" + ey.AddDate + "'," + ey.UserId + ")";
            try
            {
                iNum = SEO.DBSqlHelp.EDMDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMailHistory -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 根据指定条件删除EDMMailHistory表中数据
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        /// <returns>返回影响行数</returns>
        public int DeleteEDMMailHistoryDAL(string strSql)
        {
            int iNum = 0;
            try
            {
                iNum = SEO.DBSqlHelp.EDMDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMailHistory -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 更新EDMMailHistory表中数据
        /// </summary>
        /// <param name="ey">实例化EDMMailHistory类</param>
        /// <returns>返回影响行数</returns>
        public int UpdateEDMMailHistoryDAL(SEO.Model.EDMMailHistory ey)
        {
            int iNum = 0;
            string strSql = "update EDMMailHistory set success_num=" + ey.SuccessNum + ",fail_num=" + ey.FailNum + ",invalid_num=" + ey.InvalidNum + ",add_date='" + ey.AddDate + "',user_id=" + ey.UserId + " where history_id=" + ey.HistoryId;
            try
            {
                iNum = SEO.DBSqlHelp.EDMDBHelper.ExecuteSql(strSql);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMailHistory -> " + ex.Message);
            }
            return iNum;
        }

        /// <summary>
        /// 绑定表EDMMailHistory到DataGridView
        /// <param name="dgv">DataGridView控件名称</param>
        /// <param name="strSql">SQL语句</param>
        /// </summary>
        public void BindEDMMailHistoryDAL(System.Windows.Forms.DataGridView dgv, string strSql)
        {
            try
            {
                //清除最后自动生成的行
                dgv.AutoGenerateColumns = false;
                SqlConnection cn = new SqlConnection(SEO.DBSqlHelp.EDMDBHelper.ConnectionEDM());
                cn.Open();
                SqlCommand cm = new SqlCommand(strSql, cn);
                SqlDataAdapter da = new SqlDataAdapter(cm);
                DataSet ds = new DataSet();
                da.Fill(ds, "EDMMailHistory");
                //ds.Tables["表名"].Columns.Add("字段名", typeof(string));
                for (int i = 0; i < ds.Tables["EDMMailHistory"].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables["EDMMailHistory"].Rows[i];
                    //dr["字段名"] = "字段值";
                }
                dgv.DataSource = ds.Tables["EDMMailHistory"].DefaultView;
                da.Dispose();
                cn.Close();
                //清除当前选择行行为
                dgv.CurrentCell = null;

            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("绑定EDMMailHistory -> " + ex.Message);
            }
        }

    }
}
